package com.example.myandroidapp1.dto;

import java.io.InputStream;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ImageLoader extends AsyncTask<String, Integer, Bitmap> {

	private ImageView imageView;
	private ProgressBar progressBar;
	private TextView textView;

	public ImageLoader(ImageView imageView, ProgressBar progressBar, TextView textView) {
		this.imageView = imageView;
		this.progressBar = progressBar;
		this.textView = textView;
	}

	@Override
	protected Bitmap doInBackground(String... url) {
		String image_url = url[0]; // "http://api.androidhive.info/images/sample.jpg";
		this.textView.setText(image_url);

		try {
			Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(image_url).getContent());
			return bitmap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Bitmap result) {
		progressBar.setVisibility(View.INVISIBLE);
		imageView.setImageBitmap(result);

	}

}